<?php

namespace App\Http\Controllers\MiniAPP;

use App\Enums\MiniAPP\AssistantReserveStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\MiniAPP\Fake\AssistantReserveRequest;
use App\Http\Resources\MiniAPP\Fake\AssistantReserveResource;
use App\Http\Resources\MiniAPP\Fake\AssistantResource;
use App\Http\Resources\MiniAPP\Fake\AssistantTypeResource;
use App\Models\FakeAssistant;
use App\Models\FakeAssistantReserve;
use App\Models\FakeAssistantType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AssistantController extends Controller
{
    public function getTypeList()
    {
        $types = FakeAssistantType::latest()->get();

        return AssistantTypeResource::collection($types);
    }

    public function getAssistantList(Request $request)
    {
        $query = FakeAssistant::latest()->with('type');

        if (!empty($request->fake_assistant_type_id)) {
            $query = $query->where('fake_assistant_type_id', $request->fake_assistant_type_id);
        }

        if (!empty($request->name)) {
            $query = $query->where('name', 'like', '%'.$request->name.'%');
        }

        $assistants = $query->paginate();

        return AssistantResource::collection($assistants);
    }

    public function getAssistantInfo(FakeAssistant $assistant)
    {
        return new AssistantResource($assistant);
    }

    public function getCanReserveTime(FakeAssistant $assistant, Request $request)
    {
        //获取日期
        $date = $request->date ?? now()->toDateString();

        //循环获取0到23点的时间
        $times = [];
        for ($i = 0; $i < 24; $i++) {

            $buf = [
                'time' => $i,
                'can_reserve' => true,
                'end_time' => $i + 1,
            ];

            if (FakeAssistantReserve::where([
                'fake_assistant_id' => $assistant->id,
                'date' => $date,
                'time' => $i,
            ])->exists()) {
                $buf['can_reserve'] = false;
            }

            $times[] = $buf;
        }

        return ApiResponseHelper::json($times);
    }

    public function reserve(FakeAssistant $assistant, AssistantReserveRequest $request)
    {
        if (FakeAssistantReserve::where([
            'fake_assistant_id' => $assistant->id,
            'date' => $request->date,
            'time' => $request->time,
        ])->exists()) {
            return ApiResponseHelper::json([], 400, '该时间段已被预约');
        }

        $reserve = FakeAssistantReserve::create($request->only([
            'store_id', 'name', 'phone', 'date', 'time',
        ]) + [
                'fake_assistant_id' => $assistant->id,
                'member_id' => Auth::id(),
                'status' => AssistantReserveStatus::Pending,
            ]);

        return new AssistantReserveResource($reserve);
    }

    public function getReserveList(Request $request)
    {
        $reserves = FakeAssistantReserve::with(['assistant.type', 'store'])
            ->where('member_id', Auth::id())
            ->latest();

        if (!empty($request->status)) {
            $reserves = $reserves->where('status', $request->status);
        }

        $reserves = $reserves->paginate();

        return AssistantReserveResource::collection($reserves);
    }
}
